﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using LBoL.Base;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.Core.Cards;
using LBoL.Core.Randoms;
using LBoL.Core.StatusEffects;
using LBoL.EntityLib.StatusEffects.Koishi;
using LBoL.EntityLib.StatusEffects.Neutral.Blue;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;
using ProbeMod.StatusEffects;

namespace ProbeMod.Cards
{
    public sealed class ProbeXieDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Uncommon;
            config.Type = CardType.Skill;
            config.TargetType = TargetType.Nobody;

            config.Colors = new List<ManaColor>() { ManaColor.White };
            config.Cost = new ManaGroup() { White = 1 ,Any =2 };
            config.UpgradedCost = new ManaGroup() { White = 1 };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    /// <summary>
    /// 谐
    /// </summary>
    [EntityLogic(typeof(ProbeXieDef))]
    public sealed class ProbeXie : ProbeCard
    {
        public int Gold => 10;

        public override Interaction Precondition()
        {

            List<ProbeXie> list = Library.CreateCards<ProbeXie>(2, IsUpgraded).ToList();
            var consider = list[0];
            var consider2 = list[1];
            consider.ChoiceCardIndicator = 1;
            consider2.ChoiceCardIndicator = 2;
            consider.SetBattle(base.Battle);
            consider2.SetBattle(base.Battle);

            return GameRun.Money < Gold ? null : new MiniSelectCardInteraction(list);
        }

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            var tool = base.Battle.RollCard(new CardWeightTable(RarityWeightTable.OnlyCommon, OwnerWeightTable.Valid, CardTypeWeightTable.OnlyTool));
            yield return gainTool();

            var select = ((MiniSelectCardInteraction)precondition)?.SelectedCard;
            if (select != null)
            {
                if (select.ChoiceCardIndicator == 1)
                {
                    yield return null;
                }
                else
                {
                    GameRun.LoseMoney(Gold);
                    yield return gainTool();
                }
            }
        }

        private BattleAction gainTool()
        {
            var tool = base.Battle.RollCard(new CardWeightTable(RarityWeightTable.OnlyCommon, OwnerWeightTable.Valid, CardTypeWeightTable.OnlyTool));
            tool.IsCopy = true;
            tool.DeckCounter = 1;
            return new AddCardsToHandAction(tool);
        }
    }
}
